package com.biostime.qdingding.modules.qd.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.modules.qd.dao.DashboardDao;
import com.biostime.qdingding.modules.qd.entity.Dashboard;

/**
 * 仪表盘Service
 * @author phil
 * @version 2016-03-30
 */
@Service
@Transactional(readOnly = true)
public class DashboardService extends CrudService<DashboardDao, Dashboard>  {
	
	public Dashboard getDashboard(){
		Dashboard db = new Dashboard();
		dataScopeFilter(db, "dsf", "id=a.center_id", "id=a.create_by");
		
		//会员数量
		Map<String,Object> result = dao.getMemberCount(db);
		
		if(result!=null && result.get("member_count")!=null){
			db.setMemberCount(StringUtils.toInteger(result.get("member_count")));
		}
		
		//耗课时数
		Map<String,Object> result2 = dao.getCourseCount(db);
		
		if(result2!=null && result2.get("course_count")!=null){
			db.setCourseCount(StringUtils.toInteger(result2.get("course_count")));
		}
		
		//评分
		List<Map<String, Object>> result3 = dao.getComment(db);
		
		if(result3!=null && result3.size()==2){
			db.setComment(result3);
		}
		
		//总耗课率
		Map<String,Object> result4 = dao.getTotalCourseRate(db);
		if(result4!=null && result4.get("money")!=null && result4.get("money1")!=null){
			db.setTotalCourseRate(result4);
		}
		
		//本年耗课率
		Map<String,Object> result5 = dao.getYearCourseRate(db);
		if(result5!=null && result5.get("money")!=null && result5.get("money1")!=null){
			db.setYearCourseRate(result5); 
		}
		
		//总出勤率
		Map<String,Object> result6 = dao.getTotalAttendRate(db);
		if(result6!=null && result6.get("signCount")!=null && result6.get("orderCount")!=null){
			db.setTotalAttendRate(result6);
		}
		
		//本年出勤率
		Map<String,Object> result7 = dao.getYearAttendRate(db);
		if(result7!=null && result7.get("signCount")!=null && result7.get("orderCount")!=null){
			db.setYearAttendRate(result7); 
		}
		
		//去年新签合同趋势
		List<Map<String,Object>> result8 = dao.getLastYearContract(db);
		if(result8!=null && result8.size()==12){
			db.setLastYearContract(result8);
		}
		
		//本年新签合同趋势
		List<Map<String,Object>> result9 = dao.getThisYearContract(db);
		if(result9!=null && result9.size()==12){
			db.setThisYearContract(result9); 
		}
		
		return db;
	}
}
